package org.lora.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * Title:DateUtil
 * </p>
 * <p>
 * Desription:日期工具类
 * </p>
 * Create Time:2016年7月6日 下午12:36:18
 * 
 * 
 * String strDate = "2013-06-28";</br> Date date = null;</br> try {</br> date =
 * getFormatDate(YYYY_MM_DD, strDate);</br> } catch (ParseException e) {</br>
 * e.printStackTrace();</br> }</br>
 * 
 * System.out.println(strDate + " 是一年的第几周？" + getWeekOfYear(date));</br>
 * System.out.println(strDate + " 所在周周一是？" +
 * getFormatDateStr(YYYY_MM_DD,getMondayOfWeek(date)));</br>
 * System.out.println(strDate + " 所在周周日是？" +
 * getFormatDateStr(YYYY_MM_DD,getSundayOfWeek(date)));</br>
 * System.out.println(strDate + " 当月第一天日期？" +
 * getFormatDateStr(YYYY_MM_DD,getFirstDateOfMonth(date)));</br>
 * System.out.println(strDate + " 当月最后一天日期？" +
 * getFormatDateStr(YYYY_MM_DD,getLastDateOfMonth(date)));</br>
 * System.out.println(strDate + " 当月天数？" + getDayOfMonth(date));</br>
 * System.out.println(strDate + " 当月已过多少天？" + getPassDayOfMonth(date));</br>
 * System.out.println(strDate + " 当月剩余多少天？" + getRemainDayOfMonth(date));</br>
 * System.out.println(strDate + " 所在季度第一天日期？" +
 * getFormatDateStr(YYYY_MM_DD,getFirstDateOfSeason(date)));</br>
 * System.out.println(strDate + " 所在季度最后一天日期？" +getFormatDateStr(YYYY_MM_DD,
 * getLastDateOfSeason(date)));</br> System.out.println(strDate + " 所在季度天数？" +
 * getDayOfSeason(date));</br> System.out.println(strDate + " 所在季度已过多少天？" +
 * getPassDayOfSeason(date));</br> System.out.println(strDate + " 所在季度剩余多少天？" +
 * getRemainDayOfSeason(date));</br> System.out.println(strDate + " 是第几季度？" +
 * getSeason(date));</br>
 * 
 * @author Bladnir@outlook.com
 *         <p>
 *         History:
 *         </p>
 *         2016年7月6日 Bladnir Create</br>
 */
public class DateUtil {

	public static final String YYYY_MM_DD = "yyyy-MM-dd";

	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * <code>FIRST_DAY_OF_WEEK</code> - 中国周一是一周的第一天
	 */
	public static final int FIRST_DAY_OF_WEEK = Calendar.MONDAY;

	/**
	 * 构造方法
	 */
	private DateUtil() {

	}

	/**
	 * long型 时间转换为时间字符串
	 * 
	 * @param pattern
	 *            格式化字符串
	 * @param time
	 *            long型 时间
	 * @return 指定时间对象的时间对象
	 */
	public static String getFormatDateLong(String pattern, long time) {

		if (time == 0) {
			return StringUtil.BLANK;
		} else {
			Date date = new Date(time);
			return getFormatDateStr(pattern, date);
		}

	}

	/**
	 * 时间对象转换为时间字符串
	 * 
	 * @param pattern
	 *            格式化字符串
	 * @param date
	 *            时间对象
	 * 
	 * @return 指定时间对象的时间对象
	 */
	public static String getFormatDateStr(String pattern, Date date) {
		String str = "";
		if (date != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
			str = dateFormat.format(date);
		}
		return str;
	}

	/**
	 * 时间字符串转换成时间对象
	 * 
	 * @param pattern
	 *            格式化字符串
	 * @param str
	 *            时间字符串
	 * 
	 * @return 指定 时间字符串的时间对象
	 */
	public static Date getFormatDate(String pattern, String str) throws ParseException {
		Date date = new Date();
		if (str != null && !"".equals(str)) {
			SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
			date = dateFormat.parse(str);
		}
		return date;
	}

	/**
	 * 
	 * @param olddate
	 *            时间对象
	 * @param day
	 *            相对天数，为正数表示之后，为负数表示之前
	 * 
	 * @return 指定日期字符串n天之前或者之后的日期
	 */
	public static Date getBeforeAfterDate(Date olddate, int day) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(olddate);

		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int nowDay = cal.get(Calendar.DAY_OF_MONTH);

		int newDay = nowDay + day;

		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, newDay);

		return cal.getTime();

	}

	/**
	 * 取得一年的第几周
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekOfYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int weekOfYear = c.get(Calendar.WEEK_OF_YEAR);
		return weekOfYear - 1;
	}

	/**
	 * 根据日期取得对应周周一日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMondayOfWeek(Date date) {
		Calendar monday = Calendar.getInstance();
		monday.setTime(date);
		monday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
		monday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return monday.getTime();
	}

	/**
	 * 根据日期取得对应周周日日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getSundayOfWeek(Date date) {
		Calendar sunday = Calendar.getInstance();
		sunday.setTime(date);
		sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
		sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return sunday.getTime();
	}

	/**
	 * 取得月的剩余天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getRemainDayOfMonth(Date date) {
		int dayOfMonth = getDayOfMonth(date);
		int day = getPassDayOfMonth(date);
		return dayOfMonth - day;
	}

	/**
	 * 取得月已经过的天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getPassDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 取得月天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 取得月第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDateOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
		return c.getTime();
	}

	/**
	 * 取得月最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDateOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		return c.getTime();
	}

}
